<?php


namespace App\Http\Controllers;

use App\Models\Goods;
use App\Services\KillService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * Created By FreeAbrams
 * Date: 2022/6/22
 */
class KillController extends Controller
{
	
	function index()
	{
		$res = '没有数据!';
		return view('kill.index', compact('res'));
	}
	
	function normal(Request $request)
	{
		$goods = Goods::query()->find(1);
		$res = '没有队列，没有锁,结束';
		if ($goods->stock > 0) {
			$res = Goods::query()->where('id', 1)->decrement('stock', 1, ['sell' => DB::raw('`sell`+'.(int)1)]);
			$res = '没有队列，没有锁,执行完成';
		}
		
		return view('kill.index', compact('res'));
	}
	
	function redisLockInit()
	{
		$res = '乐观锁初始化成功!';
		$kill = (new KillService)->redisLockInit(1, 100);
		if (!$kill) {
			$res = '乐观锁初始化失败!';
		}
		return view('kill.index', compact('res'));
	}
	
	function redisLock()
	{
		$res = '乐观锁执行成功!';
		$kill = (new KillService())->redisLock(mt_rand(1, 1000), 1, 1);
		if ($kill !== true) {
			$res = '乐观锁执行失败! res= '.$kill;
		}
		return view('kill.index', compact('res'));
	}
	
	function redisQueueInit()
	{
		$res = '链表初始化成功!';
		$kill = (new KillService())->redisQueueInit(1, 100);
		if (!$kill) {
			$res = '乐观锁执行失败! res= '.$kill;
		}
		return view('kill.index', compact('res'));
	}
	
	function redisQueue()
	{
		$res = '链表执行成功!';
		$kill = (new KillService())->redisQueue(mt_rand(1, 1000), 1, 1);
		if ($kill !== true) {
			$res = '乐观锁执行失败! res= '.$kill;
		}
		return view('kill.index', compact('res'));
	}
}